<template>
  <div class="app-container">

    <el-form :model="queryParams" :inline="true" label-width="80px"  ref="queryForm">
      <el-form-item label="手机号码">
        <el-input
          v-model="queryParams.mobile"
          placeholder="手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称/姓名">
        <el-input
          v-model="queryParams.nickname"
          placeholder="昵称/姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发放创客">
        <el-input
          v-model="queryParams.sendNickname"
          placeholder="发放创客昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="全部" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.status"
            :label="dict.text"
            :value="dict.status"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发放时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="区域">
        <el-select :filterable="true" style="width: 100px" @keyup.enter.native="handleQuery"
                   v-model="queryParams.provinceId" placeholder="省" clearable size="small" @clear="provinceClear"
                   @change="provinceChange">
          <el-option
            v-for="dict in areaList"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        -
        <el-select filterable style="width: 100px" @keyup.enter.native="handleQuery" v-model="queryParams.cityId"
                   placeholder="市" clearable size="small" @clear="cityClear" @change="cityChange">
          <el-option
            v-for="dict in cityList"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        -
        <el-select filterable style="width: 100px" @keyup.enter.native="handleQuery" v-model="queryParams.districtId"
                   placeholder="区" clearable size="small">
          <el-option
            v-for="dict in districtList"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="40" class="panel-group" :data="totalData">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="peoples" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              共发放人数
            </div>
            <count-to :start-val="0" :end-val="totalData.countPeople" :duration="2600" class="card-panel-num">{{
              totalData.countPeople }} 个
            </count-to>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-money">
            <svg-icon icon-class="money" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              共发放多少钱
            </div>
            <count-to :start-val="0" :end-val="totalData.totalAmount" :decimals="2" :duration="2600"
                      class="card-panel-num">{{totalData.totalAmount}} 元
            </count-to>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-money">
            <svg-icon icon-class="money" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              已使用多少钱
            </div>
            <count-to :start-val="0" :end-val="totalData.useAmount" :decimals="2" :duration="2600"
                      class="card-panel-num">{{ totalData.useAmount }} 元
            </count-to>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="用户编号" align="center" prop="idno">
        <template slot-scope="scope">
          <el-link title="查看详情" type="primary"
                   @click="handleUserInfo({userId:scope.row.acceptuserId,role:scope.row.acceptrole})">{{scope.row.idno}}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickname"/>
      <el-table-column label="真实姓名" align="center" prop="realname">
        <template slot-scope="scope">
          <span :class="scope.row.realname?'':'text-warning'">{{ scope.row.realname?scope.row.realname:'未实名' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="mobile"/>
      <el-table-column label="区域" align="center">
        <template slot-scope="scope">
          <span>{{ areaFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发放金额(元)" align="center" prop="denomination">
        <template slot-scope="scope">
          <span>{{ scope.row.denomination|currencyFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发放创客" align="center" prop="sendNickname">
        <template slot-scope="scope">
          <el-link title="查看详情" type="primary"
                   @click="handleUserInfo({userId:scope.row.makeruserId,role:scope.row.sendRole})">
            {{scope.row.sendNickname}}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="发放时间" align="center" prop="createtime" width="180"/>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <UserInfo :show="userInfoShow" @close="handleUserInfoClose" v-if="userItem" :item="userItem"></UserInfo>
  </div>
</template>

<script>
  import {listSendlog, getSendlog} from "@/api/maker/sendlog";
  import CountTo from 'vue-count-to';
  import {area} from "@/utils/area";
  import UserInfo from '@/views/components/user';
  import mixin from '@/views/components/user/mixin';

  export default {
    mixins: [mixin],
    components: {
      CountTo, UserInfo
    },
    data() {
      return {
        areaList: [],
        cityList: [],
        districtList: [],
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        postList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 日期范围
        dateRange: [],
        //统计
        totalData: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          mobile: undefined,
          nickname: undefined,
          idno: undefined,
          status: undefined,
          startTime: undefined,
          endTime: undefined,
          provinceId: undefined,
          cityId: undefined,
          districtId: undefined,
          sendNickname: undefined
        }
      };
    },
    created() {
      this.getList();
      this.statusOptions = [{status: 0, text: '未使用'}, {status: 1, text: '已使用'}, {status: 2, text: '已过期'}];
      this.getTotalInfo();
      area().then(response => {
        this.areaList = response;
      });
    },
    methods: {
      provinceClear() {
        this.queryParams.cityId = '';
        this.queryParams.districtId='';
        this.cityList = [];
        this.districtList=[];
      },
      provinceChange(value) {
        if (value) {
          this.queryParams.cityId = '';
          this.queryParams.districtId='';
          this.cityList = this.areaList.filter(item => item.value === value)[0].children;
        }
      },
      cityChange(value){
        if (value) {
          this.districtList = this.cityList.filter(item => item.value === value)[0].children;
        }
      },
      cityClear(){
        this.queryParams.districtId='';
        this.districtList=[];
      },
      getList() {
        this.loading = true;
        listSendlog(this.queryParams).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getTotalInfo() {
        this.loading = true;
        getSendlog(this.queryParams).then(response => {
          this.totalData = response.data;
          this.loading = false;
        });

      },
      areaFormat(row){
        let area=[];
        if(row.province){
          area.push(row.province);
        }
        if(row.city){
          area.push(row.city);
        }
        if(row.district){
          area.push(row.district);
        }
        return area.join('-');
      },
      // 岗位状态字典翻译
      statusFormat(row, column) {
        if (null === row.status)
          return '';
        return this.statusOptions[row.status].text
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          mobile: undefined,
          nickname: undefined,
          idno: undefined,
          status: undefined,
          dateRange: []
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        if (this.dateRange) {
          this.queryParams.startTime = this.dateRange[0];
          this.queryParams.endTime = this.dateRange[1];
        } else {
          this.queryParams.startTime = undefined;
          this.queryParams.endTime = undefined;
        }
        this.queryParams.pageNum = 1;
        this.getList();
        this.getTotalInfo();
      }
    }
  };
</script>


<style lang="scss" scoped>
  .panel-group {
    margin-top: 18px;

    .card-panel-col {
      margin-bottom: 32px;
    }

    .card-panel {
      height: 108px;
      cursor: pointer;
      font-size: 12px;
      position: relative;
      overflow: hidden;
      color: #666;
      background: #fff;
      box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
      border-color: rgba(0, 0, 0, .05);

      &:hover {
        .card-panel-icon-wrapper {
          color: #fff;
        }

        .icon-people {
          background: #40c9c6;
        }

        .icon-message {
          background: #36a3f7;
        }

        .icon-money {
          background: #f4516c;
        }

        .icon-shopping {
          background: #34bfa3
        }
      }

      .icon-people {
        color: #40c9c6;
      }

      .icon-message {
        color: #36a3f7;
      }

      .icon-money {
        color: #f4516c;
      }

      .icon-shopping {
        color: #34bfa3
      }

      .card-panel-icon-wrapper {
        float: left;
        margin: 14px 0 0 14px;
        padding: 16px;
        transition: all 0.38s ease-out;
        border-radius: 6px;
      }

      .card-panel-icon {
        float: left;
        font-size: 48px;
      }

      .card-panel-description {
        float: right;
        font-weight: bold;
        margin: 26px;
        margin-left: 0px;

        .card-panel-text {
          line-height: 18px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 16px;
          margin-bottom: 12px;
        }

        .card-panel-num {
          font-size: 20px;
        }
      }
    }
  }

  @media (max-width: 550px) {
    .card-panel-description {
      display: none;
    }

    .card-panel-icon-wrapper {
      float: none !important;
      width: 100%;
      height: 100%;
      margin: 0 !important;

      .svg-icon {
        display: block;
        margin: 14px auto !important;
        float: none !important;
      }
    }
  }
</style>
